package masterjava.common.client.gxt.base.tree;

import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.google.gwt.user.client.rpc.AsyncCallback;
import masterjava.common.shared.entity.BaseEntity;

import java.util.List;


/**
 * User: gkislin
 * Date: 8/1/11
 */
public class GenericTreeLoader<E extends BaseEntity> extends BaseTreeLoader<GenericTreeModel> {
    public GenericTreeLoader(NodeLoader<E> nodeLoader) {
        super(createProxy(nodeLoader));
    }

    private static <E extends BaseEntity> DataProxy<List<? extends GenericTreeModel>> createProxy(final NodeLoader<E> nodeLoader) {
        return new RpcProxy<List<? extends GenericTreeModel>>() {
            @Override
            protected void load(Object loadConfig, AsyncCallback<List<? extends GenericTreeModel>> treeCallback) {
                if (loadConfig == null) {
                    treeCallback.onSuccess(nodeLoader.getRootList());
                } else {
                    nodeLoader.processNodeList((GenericTreeModel) loadConfig, treeCallback);
                }
            }
        };
    }

    @Override
    public boolean hasChildren(GenericTreeModel parent) {
        return parent instanceof FolderModel;
    }
}
